﻿/*
COPYRIGHT (C) 2011-2012 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

function UserList(param) {
    var that = this;
    var opts = param;
    var aUrlParam = new Array({ Name: 'SetUsersListFilter', Controller: "Home", Area: "", Url: "" },
        { Name: 'CheckUsers', Controller: "Home", Area: "", Url: "" },
        { Name: 'SynchronizeTFSUsers', Controller: "Home", Area: "", Url: "" },
        { Name: 'GetWorkItems', Controller: "Home", Area: "", Url: "" },
        { Name: 'GetCheckOutUserList', Controller: "Home", Area: "", Url: "" }
        );

    this.ReadyJS = function () {
       // $(document).ajaxStart(BlockUi()).ajaxStop($.unblockUI());
        CallService('POST', opts.resolveUrl, { listToResolve: aUrlParam }, true,
            function (msg) {
                aUrlParam = msg;
                var urlSetFilter = aUrlParam[0].Url;
                var urlCheck = aUrlParam[1].Url;
                var urlSync = aUrlParam[2].Url;
                $(opts.isFiltered == 1 ? "#radio1" : "#radio2").attr("checked", "checked");
                $("#radio").buttonset();
                $("#radio1,#radio2").click(function () {
                    CallService("GET", urlSetFilter, { type: this.value }, true, function () { window.location.reload(true); });
                });
                $("#checkusers").click(function () {
                    return that.dialogCheckUsers(urlCheck, urlSync);
                });
                $.each($(".wip"), function () {
                    var self = $(this);
                    var id = (self[0].id).substring(3, 8);
                    self.load(aUrlParam[3].Url + '/' + id, function () {
                        $("#imgldw" + id).hide();
                        $(this).parent().show('highlight', {}, 1000, null);
                    });
                });
                $.each($(".pcs"), function () {
                    var self = $(this);
                    var id = (self[0].id).substring(3, 8);
                    self.load(aUrlParam[4].Url + '/' + id, function () {
                        $("#imgldp" + id).hide();
                        $(this).parent().show('highlight', {}, 1000, null);
                    });
                });
            });
    };

    this.dialogCheckUsers = function (urlCheck, urlSync) {
        $("<div></div>").dialog({
            title: "Check user",
            resizable: false,
            modal: true,
            width: 350,
            height: 250,
            buttons: {
                close: function () {
                    $(this).dialog("close");
                },
                "Apply": function () {
                    var dlg = $(this);
                    dlg.html('<center style="margin-top:70px"><span>Processing... </span><img  src="' + window.g_ImgDirectory + 'ajax-loader-l.gif" alt="Loading..."/></center>');
                    CallService("POST", urlSync, null, true, function () { dlg.dialog("close"); window.location.reload(true); });
                }
            },
            closeOnEscape: false,
            open: function () {
                var dlg = $(this);
                dlg.html('<center id="chkusr" style="margin-top:70px"><span>Checking users... </span><img  src="' + window.g_ImgDirectory + 'ajax-loader-l.gif" alt="Loading..."/></center>');
                CallService("GET", urlCheck, null, true, function (data) {
                    $("#chkusr").hide();
                    if (data > 0) {
                        dlg.html('<center style="margin-top:60px"> There is possibly <b>' + data + ' changes </b>between Scrumpilot users and TFS users. Click on Apply to synchronise them.</center>');
                    }
                    else {
                        dlg.html('<center style="margin-top:70px">No change</center>');
                    }
                });
            }
        });
        return false;
    };

    return this;
}
